package com.google.android.exoplayer2.upstream;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import androidx.annotation.Nullable;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
import o.alf;

public final class ContentDataSource extends alf {
    private final ContentResolver ˊ;
    @Nullable
    private Uri ˋ;
    @Nullable
    private FileInputStream ˎ;
    @Nullable
    private AssetFileDescriptor ˏ;
    private long ॱ;
    private boolean ॱॱ;

    public static class ContentDataSourceException extends IOException {
        public ContentDataSourceException(IOException iOException) {
            super(iOException);
        }
    }

    public ContentDataSource(Context context) {
        super(false);
        this.ˊ = context.getContentResolver();
    }

    public long ˋ(DataSpec dataSpec) throws ContentDataSourceException {
        long j = -1;
        try {
            this.ˋ = dataSpec.ˎ;
            ˊ(dataSpec);
            this.ˏ = this.ˊ.openAssetFileDescriptor(this.ˋ, "r");
            if (this.ˏ == null) {
                throw new FileNotFoundException("Could not open file descriptor for: " + this.ˋ);
            }
            this.ˎ = new FileInputStream(this.ˏ.getFileDescriptor());
            long startOffset = this.ˏ.getStartOffset();
            startOffset = this.ˎ.skip(dataSpec.ॱॱ + startOffset) - startOffset;
            if (startOffset != dataSpec.ॱॱ) {
                throw new EOFException();
            }
            if (dataSpec.ᐝ != -1) {
                this.ॱ = dataSpec.ᐝ;
            } else {
                long length = this.ˏ.getLength();
                if (length == -1) {
                    FileChannel channel = this.ˎ.getChannel();
                    length = channel.size();
                    if (length != 0) {
                        j = length - channel.position();
                    }
                    this.ॱ = j;
                } else {
                    this.ॱ = length - startOffset;
                }
            }
            this.ॱॱ = true;
            ˎ(dataSpec);
            return this.ॱ;
        } catch (IOException e) {
            throw new ContentDataSourceException(e);
        }
    }

    public int ˋ(byte[] bArr, int i, int i2) throws ContentDataSourceException {
        if (i2 == 0) {
            return 0;
        }
        if (this.ॱ == 0) {
            return -1;
        }
        try {
            if (this.ॱ != -1) {
                i2 = (int) Math.min(this.ॱ, (long) i2);
            }
            int read = this.ˎ.read(bArr, i, i2);
            if (read != -1) {
                if (this.ॱ != -1) {
                    this.ॱ -= (long) read;
                }
                ˊ(read);
                return read;
            } else if (this.ॱ == -1) {
                return -1;
            } else {
                throw new ContentDataSourceException(new EOFException());
            }
        } catch (IOException e) {
            throw new ContentDataSourceException(e);
        }
    }

    @Nullable
    public Uri ˊ() {
        return this.ˋ;
    }

    public void ॱ() throws ContentDataSourceException {
        this.ˋ = null;
        try {
            if (this.ˎ != null) {
                this.ˎ.close();
            }
            this.ˎ = null;
            try {
                if (this.ˏ != null) {
                    this.ˏ.close();
                }
                this.ˏ = null;
                if (this.ॱॱ) {
                    this.ॱॱ = false;
                    ˏ();
                }
            } catch (IOException e) {
                throw new ContentDataSourceException(e);
            } catch (Throwable th) {
                this.ˏ = null;
                if (this.ॱॱ) {
                    this.ॱॱ = false;
                    ˏ();
                }
            }
        } catch (IOException e2) {
            throw new ContentDataSourceException(e2);
        } catch (Throwable th2) {
            this.ˎ = null;
            try {
                if (this.ˏ != null) {
                    this.ˏ.close();
                }
                this.ˏ = null;
                if (this.ॱॱ) {
                    this.ॱॱ = false;
                    ˏ();
                }
            } catch (IOException e22) {
                throw new ContentDataSourceException(e22);
            } catch (Throwable th3) {
                this.ˏ = null;
                if (this.ॱॱ) {
                    this.ॱॱ = false;
                    ˏ();
                }
            }
        }
    }
}
